package com.proton.carepatchtemp.utils;


import com.apkfuns.logutils.LogUtils;

import java.text.DecimalFormat;
import java.util.List;

public class NumUtils {

    public static float getAverage(List<Float> floats) {
        float result = 0F;
        for (Float f :
                floats) {
            result += f;
        }

        return Float.valueOf(new DecimalFormat("###,###,###.##").format(result / floats.size()));
    }

    public static float getVariance(List<Float> nums) {
        float average = getAverage(nums);
        float sum2 = 0;
        for (float f :
                nums) {
            sum2 += Math.pow((f - average), 2);
        }
        return Float.valueOf(new DecimalFormat("###,###,###.##").format(sum2 / nums.size()));
    }

    public static long getLongNum(String numStr) {
        long num = 0;
        try {
            num = Long.parseLong(numStr);
        } catch (Exception e) {
            LogUtils.e(e);
        }
        return num;
    }

    public static boolean withinTempRange(Float temp) {
        if (null == temp) {
            return false;
        }
        return 25 < temp && 45 > temp;
    }

// --Commented out by Inspection START (2016/3/15 11:21):
//    @SuppressWarnings("unused")
//    public static double getDoubleNum(String numStr) {
//        double num = 0.0;
//        try {
//            num = Double.parseDouble(numStr);
//        }catch (Exception e){
//            LogUtils.e(e);
//        }
//        return num;
//    }
// --Commented out by Inspection STOP (2016/3/15 11:21)

    public static float getFloatNum(String numStr) {
        float num = 0.0f;
        try {
            num = Float.parseFloat(numStr);
        } catch (Exception e) {
            LogUtils.e(e);
        }
        return num;
    }

    public static int getIntNum(String trim) {
        int a = 0;
        try {
            a = Integer.parseInt(trim);
        } catch (NumberFormatException e) {
            LogUtils.e(e);
        }
        return a;
    }

    public static int getBinaryIntNum(String temp) {
        int a = 0;
        try {
            a = Integer.parseInt(temp + "", 2);
        } catch (NumberFormatException e) {
            LogUtils.e(e);
        }
        return a;

    }

// --Commented out by Inspection START (2016/3/15 11:21):
//    @SuppressWarnings("unused")
//    public static long getBinaryLongNum(String temp) {
//        long num = 0;
//        try {
//            num = Long.parseLong(temp, 2);
//        }catch (Exception e){
//            LogUtils.e(e);
//        }
//        return num;
//    }
// --Commented out by Inspection STOP (2016/3/15 11:21)

/*    public static String hex2Binary(String mainStr, int max) {
        int len = mainStr.length();
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0;i<len;i++){
            String add = ParseUtils.addZero(Integer.toBinaryString(Integer.parseInt(mainStr.substring(i,i+1),16)),4);
            stringBuilder.append(add);
        }
        if(stringBuilder.length()>max){
            return stringBuilder.substring(0,max).toString();
        }
        return stringBuilder.toString();
    }*/

    public static float getThreePot(double v) {
        return Math.round(v * 1000f) / 1000f;
    }
}
